package com.iotek.bookstore.web.controlle;

import com.iotek.bookstore.modle.dao.impl.UserDaoImpl;
import com.iotek.bookstore.modle.entity.User;
import com.iotek.bookstore.modle.service.serviceimpl.UserBizImpl;
import com.iotek.bookstore.util.MD5;
import net.sf.json.JSON;
import net.sf.json.JSONArray;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/userServlet")
public class UserServlet extends CommonServlet {

    @Override
    public void doInsert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String email=req.getParameter("email");
        String userName=req.getParameter("userName");
        String password=req.getParameter("password");
        password= MD5.getMD5(password);
        User user=new User(email,userName,password);
        new UserBizImpl().insert(user);
        req.getRequestDispatcher("login.jsp").forward(req,resp);
        /*if(new UserBizImpl().insert(user)!=0){
            req.getRequestDispatcher("login.jsp").forward(req,resp);
        }else {
            req.getRequestDispatcher("register.jsp").forward(req,resp);
        }*/
    }

    @Override
    public void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    public void doUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String type=req.getParameter("type");
        if(type!=null&&type.equals("admin")){
            adminUpdate(req,resp);
        }else {
            userUpdate(req,resp);
        }

    }

    @Override
    public void doQuery(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String type=req.getParameter("type");
    if(type.equals("judge")){
        judgeName(req,resp);
    }else if(type.equals("login")){
        login(req,resp);
    }else if(type.equals("quit")){
        quit(req,resp);
    }else if(type.equals("all")){
        queryAll(req,resp);
    }
    }
    public void judgeName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String email=req.getParameter("email");
        System.out.println(email);
        List<User>users=new UserBizImpl().query(email);
        if(users.size()>0){
            //该email已经注册
            resp.getWriter().write("1");
        }else {
            //该email还未注册
            resp.getWriter().write("2");
        }
    }

    public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String email=req.getParameter("email");
        String password=req.getParameter("password");
        if(email.equals("")||password.equals("")){
            resp.getWriter().write("1");
            return;
        }
        password=MD5.getMD5(password);
        List<User>users=new UserBizImpl().query(email);
        if(users.size()>0){
            if(users.get(0).getPassword().equals(password)){
                //登录成功
                resp.getWriter().write("2");
                HttpSession session=req.getSession();
                session.setAttribute("userName",users.get(0).getUserName());
                session.setAttribute("userId",users.get(0).getId());
                session.setAttribute("jurisdiction",users.get(0).getJurisdiction());
                System.out.println("信息来自--->userServlet--->login:");
                System.out.println("用户ID："+req.getSession().getAttribute("userId")+"--权限等级："+users.get(0).getJurisdiction());
                resp.getWriter().write(String.valueOf(users.get(0).getJurisdiction()));
            }else {
                //密码错误
                resp.getWriter().write("4");
            }
        }else {
            //输入的email还未注册
            resp.getWriter().write("3");
        }
    }

    /*
    * 退出登录清空保存在Session的用户信息
    * */
    public void quit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        HttpSession session=req.getSession();
        session.setAttribute("userName",null);
        session.setAttribute("userId",null);
        session.setAttribute("jurisdiction",null);
        resp.sendRedirect("login.jsp");
    }

    /*
    * 查询所有的用户信息
    * */
    public void queryAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        List<User>userList=new UserBizImpl().queryAll();
        JSON json= JSONArray.fromObject(userList);
        System.out.println("信息来自--->userServlet--->queryAll:");
        System.out.println(json.toString());
        resp.getWriter().write(json.toString());
    }

    /*
    * 用户更改自己的用户信息
    * */
    public void userUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        int userId=(Integer)req.getSession().getAttribute("userId");
        String userName=req.getParameter("userName");
        String password=req.getParameter("password");
        password=MD5.getMD5(password);
        User user=new User();
        user.setId(userId);
        user.setUserName(userName);
        user.setPassword(password);
        if(new UserBizImpl().update(user)==0){
            resp.getWriter().write("0");

        }else {
            resp.getWriter().write("1");
            //修改信息成功后清除session中该用户的信息
            HttpSession session=req.getSession();
            session.setAttribute("userName",null);
            session.setAttribute("userId",null);
            session.setAttribute("jurisdiction",null);
        }
    }

    /*
    * 管理员更改用户信息
    * */
    public void adminUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        int userId=Integer.parseInt(req.getParameter("userId"));
        String userName=req.getParameter("userName");
        int jurisdiction=Integer.parseInt(req.getParameter("jurisdiction"));
        User user=new User();
        user.setId(userId);
        user.setUserName(userName);
        user.setJurisdiction(jurisdiction);
        System.out.println("信息来自--->userServlet--->adminUpdate:");
        System.out.println("用户ID："+userId+"---用户呢称："+userName+"---用户权限等级："+jurisdiction);
        if(new UserBizImpl().update(user)==0){
            resp.getWriter().write("0");
        }else {
            resp.getWriter().write("1");
        }

    }
}
